#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
查看Redis中的原始爬取数据
"""

import redis
import json
import argparse
from datetime import datetime

def connect_redis():
    """连接Redis"""
    redis_configs = [
        {'host': '192.168.88.130', 'port': 6379, 'password': '123456'},
        {'host': '192.168.88.130', 'port': 6379, 'password': 'redis'},
        {'host': '192.168.88.130', 'port': 6379, 'password': None},
    ]
    
    for config in redis_configs:
        try:
            r = redis.Redis(
                host=config['host'],
                port=config['port'],
                password=config['password'],
                decode_responses=True
            )
            r.ping()
            print(f"✓ Redis连接成功: {config['host']}:{config['port']}")
            return r
        except Exception as e:
            print(f"Redis连接失败: {config['host']}:{config['port']} - {e}")
    
    raise Exception("无法连接到Redis")

def view_raw_data(limit=None, show_json=False):
    """查看原始数据"""
    r = connect_redis()
    
    # 获取所有价格数据键
    keys = r.keys("cnhnb_price:*")
    keys.sort()  # 按时间排序
    
    if limit:
        keys = keys[-limit:]  # 取最新的N条
    
    print(f"\n=== Redis中的原始数据 ===")
    print(f"总数据量: {len(keys)} 条")
    print(f"显示: {'最新' + str(limit) + '条' if limit else '全部'} 数据")
    print("=" * 80)
    
    for i, key in enumerate(keys, 1):
        try:
            # 获取原始数据
            raw_data = r.get(key)
            if raw_data:
                # 尝试解析JSON
                try:
                    data = json.loads(raw_data)
                    print(f"\n{i}. 键: {key}")
                    print(f"   时间戳: {key.split(':')[-1]}")
                    print(f"   产品名称: {data.get('name', 'N/A')}")
                    print(f"   价格: {data.get('price_value', 'N/A')} 元/{data.get('unit', 'N/A')}")
                    print(f"   原始文本: {data.get('price_original', 'N/A')}")
                    print(f"   来源URL: {data.get('source_url', 'N/A')}")
                    print(f"   爬取时间: {data.get('crawl_timestamp', 'N/A')}")
                    
                    if show_json:
                        print(f"   完整JSON: {json.dumps(data, ensure_ascii=False, indent=2)}")
                    
                except json.JSONDecodeError:
                    print(f"\n{i}. 键: {key}")
                    print(f"   原始数据: {raw_data}")
            
        except Exception as e:
            print(f"\n{i}. 键: {key} - 读取失败: {e}")
    
    print("\n" + "=" * 80)

def export_to_file(filename=None):
    """导出数据到文件"""
    r = connect_redis()
    keys = r.keys("cnhnb_price:*")
    keys.sort()
    
    data_list = []
    for key in keys:
        try:
            raw_data = r.get(key)
            if raw_data:
                data = json.loads(raw_data)
                data['redis_key'] = key
                data_list.append(data)
        except:
            continue
    
    if filename:
        with open(filename, 'w', encoding='utf-8') as f:
            json.dump(data_list, f, ensure_ascii=False, indent=2)
        print(f"✓ 数据已导出到: {filename}")
    else:
        filename = f"cnhnb_data_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
        with open(filename, 'w', encoding='utf-8') as f:
            json.dump(data_list, f, ensure_ascii=False, indent=2)
        print(f"✓ 数据已导出到: {filename}")
    
    return data_list

def main():
    parser = argparse.ArgumentParser(description='查看Redis中的原始爬取数据')
    parser.add_argument('-l', '--limit', type=int, help='显示最新N条数据')
    parser.add_argument('-j', '--json', action='store_true', help='显示完整JSON')
    parser.add_argument('-e', '--export', type=str, help='导出数据到文件')
    
    args = parser.parse_args()
    
    if args.export:
        export_to_file(args.export)
    else:
        view_raw_data(args.limit, args.json)

if __name__ == '__main__':
    main() 